package com.project.service.impl;

import com.project.bean.AddressBookBean;
import com.project.bean.CutPageBean;
import com.project.service.IAddressBookService;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

public class AddresBookImplService extends BaseService implements IAddressBookService {
    @Override
    public void add(AddressBookBean people) {
        this.mapper.add(people);
        this.commitAndClose();
    }

    @Override
    public void del(int id) {
        this.mapper.del(id);
        this.commitAndClose();
    }

    @Override
    public void delMore(int[] idArray) {
        this.mapper.delMore(idArray);
        this.commitAndClose();
    }

    @Override
    public void update(int id, String tel) {
        this.mapper.update(id,tel);
        this.commitAndClose();
    }

    @Override
    public List<AddressBookBean> findAll() {
        this.flag=false;
        List<AddressBookBean> list = this.mapper.findAll();
        this.commitAndClose();
        return list;
    }

    @Override
    public AddressBookBean findById(int id) {
        this.flag=false;
        AddressBookBean address = this.mapper.findById(id);
        this.commitAndClose();
        return address;
    }

    @Override
    public List<AddressBookBean> findByName(String name) {
        this.flag=false;
        List<AddressBookBean> list = this.mapper.findByName("%"+name+"%");
        this.commitAndClose();
        return list;
    }

    @Override
    public List<AddressBookBean> findByBirthday(String startDate, String endDate) {
        this.flag=false;
        List<AddressBookBean> list = this.mapper.findByBirthday(startDate,endDate);
        this.commitAndClose();
        return list;
    }

    @Override
    public void addMore(List<AddressBookBean> list) {
        this.mapper.addMore(list);
        this.commitAndClose();
    }

    @Override
    public List<AddressBookBean> findByItem(String name, String startDate, String endDate) {
        this.flag=false;
        List<AddressBookBean> list =  this.mapper.findByItem(name,startDate,endDate);
        this.commitAndClose();
        return list;
    }

    @Override
    public CutPageBean<AddressBookBean> findAll(int pageNo) {
        this.flag=false;
        CutPageBean<AddressBookBean> cutPageBean = new CutPageBean<AddressBookBean>();

        cutPageBean.setList(mapper.cutAllList((pageNo-1)*CutPageBean.PAGESIZE,CutPageBean.PAGESIZE));
        cutPageBean.setCount(mapper.cutAllCount());
        this.commitAndClose();
        return cutPageBean;
    }

    @Override
    public CutPageBean<AddressBookBean> cutByItem(int pageNo, String name, String startDate, String endDate) {
        this.flag=false;
        CutPageBean<AddressBookBean> cutPageBean = new CutPageBean<AddressBookBean>();
        cutPageBean.setList(mapper.cutByItem(name,startDate,endDate,
                (pageNo-1)*CutPageBean.PAGESIZE,CutPageBean.PAGESIZE));
        cutPageBean.setCount(mapper.cuntByCount(name,startDate,endDate));
        this.commitAndClose();
        return cutPageBean;
    }

    public static void main(String[] args){
        AddresBookImplService service = new AddresBookImplService();
//        service.add(new AddressBookBean("李仕杰","18030405215",
//                "项目经理", LocalDate.parse("1995-08-26")));
//        service.del(6);
//        service.update(7,"19000000000");
//        service.delMore(new int[]{7,4});
//        System.out.println(service.findAll());
//        System.out.println(service.findById(8));
//        System.out.println(service.findByName("王"));
//        System.out.println(service.findByBirthday("1969-01-01","1981-02-02"));
//            service.addMore(Arrays.asList(new AddressBookBean("李仕杰","18030405215","项目经理",
//                    LocalDate.parse("1995-08-26")),
//                    new AddressBookBean("李连杰","17874523651","五大明星",
//                            LocalDate.parse("1975-08-26")),
//                    new AddressBookBean("李小龙","13336523214","神",
//                            LocalDate.parse("1925-08-26"))
//            ));
//        System.out.println(service.findByItem("李","1960-01-01","1975-09-09"));
//        System.out.println(service.findAll(2));
        System.out.println(service.cutByItem(2,"李",null,null));
    }
}
